<template>
  <div class="app-container">
    <el-table :data="data" style="width: 100%" border>
      <el-table-column prop="date" label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="description" label="中图预览" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px;" :src='scope.row.midImg' fit="fill">
            <!-- :src='scope.row.midImg2'-->
          </el-image>
        </template>
      </el-table-column>

      <el-table-column prop="description" label="大图预览" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px;" :src='scope.row.bigImg' fit="fill">
            <!-- :src='scope.row.bigImg2'-->
          </el-image>
        </template>
      </el-table-column>

      <el-table-column prop="description" width="100" label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top" :hide-after=1200>
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
              @click="getBannerObjHand(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!--弹出表单-->
    <el-dialog title="请修改信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">

        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>

        <!--图片-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="小图预览（点击修改）">
              <!--小图片-->
              <upLoad v-model="form.midImg" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="大图预览（点击修改）">
              <!--大图片-->
              <upLoad v-model="form.bigImg" />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="setBannerInfoHand">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { getBanner, setBanner } from "@/api/banner.js";
import { URL_CONFIG } from "@/urlConfig.js"
import upLoad from "@/components/UpLoad/index.vue"

export default {
  data() {
    return {
      data: [],
      dialogFormVisible: false,
      form: {
        bigImg: '',
        description: '',
        midImg: '',
        title: '',
        id: ''
      },
    }
  },
  components: {
    upLoad
  },
  created() {
    this.fetchBanner()
  },
  methods: {
    fetchBanner() {
      getBanner().then(banner_data => {
        // console.log(banner_data.data)
        this.data = banner_data.data
        // for (const item of this.data) {
        //   item.midImg2 = URL_CONFIG + item.midImg
        //   item.bigImg2 = URL_CONFIG + item.bigImg
        // }
      })
    },
    getBannerObjHand(data) {
      // console.log('frmmmmmmmmmmm', this.form)
      this.form = { ...data } //点击谁就把谁的数据给form【只有一个对象】
      this.dialogFormVisible = true
      // console.log('赋值完后的formmmmmmmmmmm', this.form)
    },
    setBannerInfoHand() {
      // 从表单里面拿到用户修改的数据，然后发送给服务器
      // setBanner 【需要传递数组】
      let arr = [...this.data]
      for (var i = 0; i < arr.length; i++) {
        console.log(this.form)
        if (arr[i].id === this.form.id) { //【id对比，然后替换arr[i]】
          arr[i] = this.form
        }
      }
      // console.log(arr, '修改后的arrrrrrrrrrrrrr')

      // 【下面代码就是被修改后的arr数组】

      // 重新设置数组到 getBanner的api【带数组】
      setBanner(arr).then(r => {
        console.log("serBanner::::", r)
        this.dialogFormVisible = false
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        // 重新获取数据【调用getBanner api】
        this.fetchBanner()
      })
    }
  }
}
</script>

<style scoped></style>
